# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Contributor: William Giokas <1007380@gmail.com>

pkgname=pypy
pkgver=7.3.13
pkgrel=1
pkgdesc="A Python implementation written in Python, JIT enabled"
url="https://pypy.org"
arch=('loong64' 'x86_64')
depends=('expat' 'bzip2' 'gdbm' 'openssl' 'libffi' 'zlib' 'ncurses')
makedepends=('pypy' 'tk' 'sqlite' 'mercurial')
optdepends=('sqlite: sqlite module'
            'tk: tk module')
options=(!buildflags)
license=('MIT')
source=("https://downloads.python.org/pypy/pypy2.7-v${pkgver}-src.zip")
sha512sums=('2bb409f2621a7e346475a5f3f8154320a46223eebe080c728c3115421fe4756666efbe6a2246eeba58362503ac2f3bfee1a7a427fe9170b42149027ee6e54570')

build() {
  cd pypy2.7-v${pkgver}-src/pypy/goal

  # For some reason, PyPy wants to use their vendored dependencies when detecting linux.
  # Weird, we'll patch it out.
  sed -i "s/, 'linux', 'linux2'//" targetpypystandalone.py
  pypy ../../rpython/bin/rpython -Ojit --shared targetpypystandalone

  # Compile binary modules
  PYTHONPATH=../.. ./pypy-c ../../lib_pypy/pypy_tools/build_cffi_imports.py
}

package() {
  cd pypy2.7-v${pkgver}-src

  # Prepare installation
  pypy pypy/tool/release/package.py --archive-name pypy --targetdir .
  mkdir unpacked
  tar xf pypy.tar.bz2 -C unpacked

  # Install pypy
  cd unpacked
  install -Dm755 pypy/bin/pypy "${pkgdir}"/opt/pypy/bin/pypy
  install -Dm755 pypy/bin/libpypy-c.so "${pkgdir}"/opt/pypy/bin/libpypy-c.so
  cp -r pypy/include pypy/lib_pypy pypy/lib-python pypy/site-packages "${pkgdir}"/opt/pypy/
  cd ..

  # Install symlink
  mkdir -p "${pkgdir}"/usr/bin "${pkgdir}"/usr/lib
  ln -s /opt/pypy/bin/pypy "${pkgdir}"/usr/bin/pypy
  ln -s /opt/pypy/bin/libpypy-c.so "${pkgdir}"/usr/lib/libpypy-c.so

  # Install misc stuff
  install -Dm644 README.rst "${pkgdir}"/opt/pypy/README.rst
  install -Dm644 LICENSE "${pkgdir}"/opt/pypy/LICENSE
  install -Dm644 LICENSE "${pkgdir}"/usr/share/licenses/pypy/LICENSE
}
# vim: ts=2 sw=2 et:
